#ifndef DECOMPRESS_CLS
#define DECOMPRESS_CLS

#include <fstream.h>
#include "const.h"
#include "type.h"

class cpifstream : public ifstream
	{
	public:

		cpifstream(void);
		cpifstream(char *fn);
		~cpifstream();

		cpifstream& operator>> (char& c)			{getbuf(&c,sizeof(char)); return *this;};
		cpifstream& operator>> (short& s)			{getbuf(&s,sizeof(short)); return *this;};
		cpifstream& operator>> (int& i)			{getbuf(&i,sizeof(int)); return *this;};
		cpifstream& operator>> (long& l)			{getbuf(&l,sizeof(long)); return *this;};
		cpifstream& operator>> (float& f)			{getbuf(&f,sizeof(float)); return *this;};
		cpifstream& operator>> (double& d)			{getbuf(&d,sizeof(double)); return *this;};
		cpifstream& operator>> (long double& ld)	{getbuf(&ld,sizeof(long double)); return *this;};
		cpifstream& read(signed char* s, int n)		{getbuf(s,n); return *this;};
		cpifstream& read(unsigned char* s, int n)	{getbuf(s,n); return *this;};
		cpifstream& operator>> (char *s);

		int get(void);
		int peek(void);
		int gcount(void) {int c=count; count=0; return c;};

		cpifstream& get(signed char* s, int len, char c='\n')			{getbuf2(s,len,c); return *this;};
		cpifstream& get(unsigned char* s, int len, char c='\n')          {getbuf2(s,len,c); return *this;};
		cpifstream& get(signed char& c)							{c=get(); return *this;};
		cpifstream& get(unsigned char& c)							{c=get(); return *this;};
		cpifstream& getline(signed char* s, int len, char c='\n')		{getbuf2(s,len,c,1); return *this;};
		cpifstream& getline(unsigned char* s, int len, char c='\n')		{getbuf2(s,len,c,1); return *this;};
		cpifstream& ignore(int n=1, int delim=EOF);

		void open(char *fn);
		int done(void) {return all_done;};
		filebuf* close(void);

// The following functions are defined so that they are NO OP's:

		cpifstream& putback(char) {return *this;};
		cpifstream& seekg(long) {return *this;};
		cpifstream& seekg(long, seek_dir) {return *this;};

// ---

	private:

		void init(void);
		void clear(void);
		void decode(void);
		void getbuf(void* v, int l);
		void getbuf2(char* s, int l, char delim, char ed=0);
		int decompress(uchar *inbuff, uint inbuff_len, uchar *outbuff);

		uchar *inbuf;				// input buffer
		uchar *outbuf;				// output buffer after compression
		int idx;					// output buffer index
		int o_len;				// output buffer length
		int all_done;
		int count;
	};

#endif
